'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  //获取要编辑的相应数据
  async index() {
    const { ctx,app } = this;
    const {id}=ctx.query
    const data=await app.mysql.get("allarticles",{id})
    if(data){
      ctx.body = {
        code:200,
        data
      }
    }
  }
  //编辑后的数据
  async list(){
    const { ctx,app } = this;
    const {ipt,text,id}=ctx.request.body
    const result=await app.mysql.update("allarticles",{
      headline:ipt,
      content:text,
      id:id
    })
    if(result.affectedRows){
      ctx.body={
        code:200,
        mess:"修改成功"
      }
    }
  }
  //添加数据
  async addEditor(){
    const { ctx,app } = this;
    const {ipt,text}=ctx.request.body
    console.log(ipt,text);
    const result=await app.mysql.insert("allarticles",{
      headline:ipt,
      content:text,
      status: "草稿",
      classify: "后端",
      label:"a",
      reading: "100",
      likeing: "10",
      datetime:"2022-05-22",
      key:Math.random().toString().substr(2,4),
      id: null,
      img:"http://dummyimage.com/100x200/f2df79&text=zhang",
    })
    if(result.affectedRows){
      ctx.body={
        code:200,
        mess:"添加成功"
      }
    }
  }
}

module.exports = HomeController;
